package com.mu.securitysso.authentication;

import com.mu.securitysso.entity.Permission;
import com.mu.securitysso.entity.Role;
import com.mu.securitysso.entity.User;
import com.mu.securitysso.service.SecurityDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sht
 */
@Service
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    SecurityDataService securityDataService;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        User user = securityDataService.findSUserByName(s);

        if( user != null){
            List<Role>  roleList = securityDataService.findSRoleListBySUserId(user.getId());
            List<Permission> permissionList = securityDataService.findSPermissionListBySUserId(user.getId());
            return new MyUserDetails(user,roleList,permissionList);
        }

        return null;
    }
}
